package com.ruoyi.project.mahjong.controller;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.mahjong.domain.vo.DeskVo;
import com.ruoyi.project.system.domain.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.mahjong.domain.TOpenTable;
import com.ruoyi.project.mahjong.service.ITOpenTableService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 新开桌Controller
 *
 * @author ruoyi
 * @date 2023-10-07
 */
@RestController
@RequestMapping("/mahjong/table")
public class TOpenTableController extends BaseController
{
    @Autowired
    private ITOpenTableService tOpenTableService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 查询新开桌列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(TOpenTable tOpenTable)
    {
        startPage();
        List<TOpenTable> list = tOpenTableService.selectTOpenTableList(tOpenTable);
        return getDataTable(list);
    }
    @GetMapping("/listDetails")
    public TableDataInfo listDetails(TOpenTable tOpenTable)
    {
        SysUser user1 = SecurityUtils.getLoginUser().getUser();
        Long shopId = redisCache.getCacheObject(Constants.SHOP_SUF + user1.getUserId());
        tOpenTable.setShopId(shopId);
        startPage();
        List<DeskVo> list = tOpenTableService.selectTabels(tOpenTable);
        return getDataTable(list);
    }
    /**
     * 导出新开桌列表
     */
    @PreAuthorize("@ss.hasPermi('mahjong:table:export')")
    @Log(title = "新开桌", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TOpenTable tOpenTable)
    {
        List<TOpenTable> list = tOpenTableService.selectTOpenTableList(tOpenTable);
        ExcelUtil<TOpenTable> util = new ExcelUtil<TOpenTable>(TOpenTable.class);
        return util.exportExcel(list, "table");
    }

    /**
     * 获取新开桌详细信息
     */
    @PreAuthorize("@ss.hasPermi('mahjong:table:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tOpenTableService.selectTOpenTableById(id));
    }

    /**
     * 新增新开桌
     */
    @PreAuthorize("@ss.hasPermi('mahjong:table:add')")
    @Log(title = "新开桌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOpenTable tOpenTable)
    {
        return toAjax(tOpenTableService.insertTOpenTable(tOpenTable));
    }

    /**
     * 修改新开桌
     */
    @PreAuthorize("@ss.hasPermi('mahjong:table:edit')")
    @Log(title = "新开桌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOpenTable tOpenTable)
    {
        return toAjax(tOpenTableService.updateTOpenTable(tOpenTable));
    }

    /**
     * 删除新开桌
     */
    @PreAuthorize("@ss.hasPermi('mahjong:table:remove')")
    @Log(title = "新开桌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tOpenTableService.deleteTOpenTableByIds(ids));
    }



    @GetMapping(value = "reset/{orderSn}")
    public AjaxResult getInfo(@PathVariable("orderSn") String orderSn)
    {
        tOpenTableService.updateStatus(orderSn);
        return AjaxResult.success();
    }

}
